package com.gtxc.kotlinmvvm.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gtxc.kotlinmvvm.R;


/**
 * ClassName TitleBar
 * Description 自定义标题栏
 */
public class TitleBar extends RelativeLayout {

    private LinearLayout layout_app_title;

    private ImageView ivLeft;

    private TextView tvText;

    private RelativeLayout llTitleBar;

    private TextView tvTextRight;

    private ImageView imageViewRight;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        init(context, attrs);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(final Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_title, this);
        layout_app_title = (LinearLayout) findViewById(R.id.layout_app_title);
        ivLeft = findViewById(R.id.title_bar_iv);
        tvText = findViewById(R.id.tv_title_bar);
        llTitleBar = findViewById(R.id.ll_title_bar);
        tvTextRight = findViewById(R.id.tv_title_right);
        imageViewRight = findViewById(R.id.title_bar_right);
        parseStyle(context, attrs);


        layout_app_title.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
    public void setRightText(String text) {
        if (text != null) {
            tvTextRight.setText(text);
        }
    }
    private void parseStyle(Context context, AttributeSet attrs) {

        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.titleView);
            String title = ta.getString(R.styleable.titleView_title);
            Drawable drawable = ta.getDrawable(R.styleable.titleView_title_bg_color);
            int titlebgColor = ta.getColor(R.styleable.titleView_title_bg_color, Color.parseColor("#000000"));
            if (drawable==null){
                //如果为空，说明未设置背景色，这里就用drawable
                llTitleBar.setBackgroundColor(titlebgColor);
            }else {
                //如果不为空，说明设置了背景色，这里就用titlebgColor
                llTitleBar.setBackgroundColor(titlebgColor);
            }
            int titleColor = ta.getColor(R.styleable.titleView_titleColor, Color.parseColor("#ffffff"));
            String titleRight = ta.getString(R.styleable.titleView_title_right);
            int titleImageRight = ta.getResourceId(R.styleable.titleView_imRight, -1);

            int titleRightColor = ta.getColor(R.styleable.titleView_title_right_color, Color.parseColor("#ffffff"));
            if (titleImageRight != -1) {
                imageViewRight.setImageResource(titleImageRight);
            }
            if (title != null) {
                tvText.setText(title);
            }
            tvText.setTextColor(titleColor);
            tvTextRight.setTextColor(titleRightColor);
            if (titleRight != null) {
                tvTextRight.setText(titleRight);
            }
            ta.recycle();
        }
    }


    public void setOnLeftClickListener(OnClickListener clickListener) {
        ivLeft.setOnClickListener(clickListener);
    }

    public void setOnRightClickListener(OnClickListener clickListener) {
        tvTextRight.setOnClickListener(clickListener);
    }

    public void setTitleText(String text) {
        tvText.setText(text);
    }

    public String getTitleText() {
        return tvText.getText().toString().trim();
    }


    /**
     * 20-6-3 新增设置背景颜色 ,默认是白色的，此处是覆盖颜色
     */
    public void setTitleBackgroundColor(int color) {
        layout_app_title.setBackgroundColor(color);
    }

    /**
     * 20-6-3 新增设置左边按钮图标 ,默认是有一张图标的，此处是覆盖
     */
    public void setLeftBackgroundColor(int resId) {
        ivLeft.setImageResource(resId);
    }


}
